package com.dragon.mmochat.message;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.MmochatChenHao;
import com.dragon.mmochat.model.gang.MmochatGang;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.skill.MmochatSkill;
import com.dragon.mmochat.service.MmochatBattleService;
import com.dragon.mmochat.service.MmochatGangService;
import com.dragon.mmochat.util.MmochatUtil;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_6_2S extends SkymobiProtocolMessage {
	private MmochatPlayer player = null;

	public Mmochat_6_2S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_6_2);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		if (player == null) {
			return;
		}
		/*********** 基本信息 ***********/
		// 道行
		String dao = MmochatUtil.getDaoString(player.getDao());
		out.putPrefixedString(dao, 1);
		// 帮派
		if (player.hasGang()) {
			MmochatGang gang = MmochatGangService.getGangById(player
					.getGangId());
			if (gang != null) {
				out.putPrefixedString(gang.getName(), 1);
			} else {
				out.putPrefixedString("暂无帮派", 1);
			}
		} else {
			out.putPrefixedString("暂无帮派", 1);
		}
		// 性别
		if (player.getSexual() == 0) {
			out.putPrefixedString("男", 1);
		} else {
			out.putPrefixedString("女", 1);
		}
		// 等级
		out.putShort((short) player.getLevel());
		// 门派
		out.putPrefixedString(player.getFamily().getName(), 1);
		// 战绩
		out.putInt(player.getZhanji());

		/*********** 属性 ***********/
		out.putInt(player.getCurBlood());
		out.putInt(player.getTotalBlood());
		out.putInt(player.getCurSprit());
		out.putInt(player.getTotalSprit());
		out.putInt(player.getTotalAttackPoint());
		out.putInt(player.getTotalSpritPoint());
		out.putInt(player.getTotalBloodPoint());
		out.putInt(player.getTotalSpeedPoint());
		out.putInt(player.getPhysicalAttack());
		out.putInt(player.getSpritAttack());
		out.putInt(player.getPhysicalDefend());
		out.putInt(player.getMagicDefend());
		out.putInt(player.getSpeed());
		out.putInt(player.getSpace_point());
		out.putInt(player.getTotalAttack_gift_point());
		out.putInt(player.getTotalSprit_gift_point());
		out.putInt(player.getTotalBlood_gift_point());
		out.putInt(player.getTotalDefend_gift_point());
		out.putInt(player.getTotalSpeed_gift_point());
		out.putInt(player.getSpace_gift_point());
		out.putInt(player.getExp());
		out.putInt(MmochatBattleService.getHumanExpNeedToUplevel(player
				.getLevel(), player.getLife_turn()));
		out.putUnsignedShort(player.getExtraPointTag());
		/*********** 技能 ***********/
		List<MmochatSkill> magicSkills = new ArrayList<MmochatSkill>();
		List<MmochatSkill> limitSkills = new ArrayList<MmochatSkill>();
		for (MmochatSkill skill : player.getSkills().values()) {
			if (skill.getType().getSkillType() == 0) {
				magicSkills.add(skill);
			} else if (skill.getType().getSkillType() == 1) {
				limitSkills.add(skill);
			}
		}
		out.put((byte) magicSkills.size());
		if (magicSkills.size() > 0) {
			Collections.sort(magicSkills);
			for (MmochatSkill skill : magicSkills) {
				out.putPrefixedString(skill.getType().toString(), 1);
				out.put((byte) skill.getTargetNum(player));
			}
		}
		out.put((byte) limitSkills.size());
		if (limitSkills.size() > 0) {
			Collections.sort(limitSkills);
			for (MmochatSkill skill : limitSkills) {
				out.putPrefixedString(skill.getType().toString(), 1);
				out.put((byte) skill.getTargetNum(player));
			}
		}

		/*********** 社交信息 ***********/
		if (player.getSocialInfo() == null) {
			out.putShort((short) 0);
		} else {
			out.putPrefixedString(player.getSocialInfo(), 2);
		}
	}

	public void setPlayer(MmochatPlayer player) {
		this.player = player;
	}

}